package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.BaseCheckStoreEntity;
import com.kaili.basic.pojo.vo.BaseCanCheckStockVO;
import com.kaili.basic.pojo.vo.BaseCheckStoreExportVO;
import com.kaili.basic.pojo.vo.BaseCheckStoreVO;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.basic.service.BaseCheckStoreService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.util.List;
import java.util.UUID;

@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Transactional
@Api(tags = "盘点管理", description = "盘点管理")
public class BaseCheckStoreController {
    @Autowired
    private BaseCheckStoreService baseCheckStoreService;

    @GetMapping("/checkStore/orderNo")
    @ApiOperation(value = "查询盘点单号")
    public Long queryStoreOrderNo(){
        return baseCheckStoreService.queryStoreOrderNo();
    }

    @GetMapping("/checkStore")
    @ApiOperation(value = "分页盘点单", notes = "分页查询盘点单")
    public Page<BaseCheckStoreVO> queryPageList(@ModelAttribute BaseCheckStoreQueryDTO queryDto){
        return baseCheckStoreService.queryPageList(new Page<BaseCheckStoreVO>(queryDto.getPage(),queryDto.getPageSize()),queryDto);
    }

    @GetMapping("/checkStore/byId/{id}")
    @ApiOperation(value = "查询要盘点产品(上传)", notes = "根据Id查询可盘点产品信息")
    public BaseCheckStoreVO queryCheckStockById(@PathVariable("id") Long id){
        return baseCheckStoreService.queryCheckStockById(id);
    }

    @GetMapping("/checkStore/stockAndDetail/{id}")
    @ApiOperation(value = "根据Id查询盘点单详情", notes = "根据Id查询盘点单详情")
    public BaseCheckStoreVO queryCheckStockAndDetail(@PathVariable("id") Long id){
        return baseCheckStoreService.queryCheckStockAndDetail(id);
    }

    @GetMapping("/checkStore/detail/{id}")
    @ApiOperation(value = "根据Id查询盘点单包含产品", notes = "根据Id查询盘点单包含产品")
    public BaseCheckStoreVO queryCheckProductDetail(@PathVariable("id") Long id){
        return baseCheckStoreService.queryCheckProductDetail(id);
    }

    @GetMapping("/checkStore/stock")
    @ApiOperation(value = "查询可盘点产品", notes = "查询可盘点产品")
    public Page<BaseCanCheckStockVO>  queryCanCheckStock(@ModelAttribute BaseCanCheckStockDTO queryDto){
        return baseCheckStoreService.queryCanCheckStock(new Page<BaseCanCheckStockVO>(queryDto.getPage(),queryDto.getPageSize()),queryDto);
    }

    @GetMapping("/checkStore/export")
    @ApiOperation(value = "盘点明细导出")
    public void checkStoreExport(HttpServletResponse response, @ModelAttribute BaseCheckStoreQueryDTO queryDto) throws RestException {
        List<BaseCheckStoreExportVO> list = baseCheckStoreService.checkStoreExport(queryDto);
        ExcelUtil.exportExcel(list,"盘点管理","sheet1", BaseCheckStoreExportVO.class, UUID.randomUUID().toString()+".xls",response);
    }

    @PostMapping("/checkStore")
    @ApiOperation(value = "新增盘点")
    public RestStatus addCheckStock(@RequestBody BaseCheckStoreAddDTO addDto){
        return baseCheckStoreService.addCheckStock(addDto);
    }

    @PutMapping("/checkStore")
    @ApiOperation(value = "修改盘点")
    public RestStatus updateCheckStock(@RequestBody BaseCheckStoreEditDTO editDTO){
        return baseCheckStoreService.updateCheckStock(editDTO);
    }

    @PutMapping("/checkStore/audit")
    @ApiOperation(value = "审核盘点")
    public RestStatus auditCheckStock(@RequestBody BaseCheckStoreAuditDTO auditDTO){
        return baseCheckStoreService.auditCheckStock(auditDTO);
    }

    @PutMapping("/checkStore/upload")
    @ApiOperation(value = "上传盘点")
    public RestStatus uploadCheckStock(@RequestBody List<BaseCheckStockAddDTO> list){
        return baseCheckStoreService.uploadCheckStock(list);
    }

    @DeleteMapping("/checkStore/{id}")
    @ApiOperation(value = "删除盘点")
    public RestStatus deleteCheckStock(@ApiParam("订单号id")@PathVariable("id") long id){
        BaseCheckStoreEntity entity=new BaseCheckStoreEntity();
        entity.setId(id);
        entity.setStatus("0");
        baseCheckStoreService.updateById(entity);
        return CommonStatus.DELETE_OK;
    }
}


